var change = document.querySelectorAll(".btn-change")
var deletee = document.querySelectorAll(".btn-delete")
var reset = document.querySelectorAll(".btn-reset")
var username

for(var i = 0,j = change.length;i < j;i++)
{
    change[i].addEventListener('click',onChangeClick)
    deletee[i].addEventListener('click',onDeleteClick)
    reset[i].addEventListener('click',onResetClick)
}
function onChangeClick()
{
    $("#editPassword").modal('show')
    username = this.getAttribute("id").split(':').at(1)
    console.log(this)
}

function onNewPasswordSummit()
{
    let form = document.querySelector("#pwdForm")
    let formData = new FormData(form)
    let pwd = formData.get('newPwd')
    if(pwd.length === 0)
    {
        document.querySelector("#modalMsg").innerHTML = '密码不能为空，请重新输入'
        document.querySelector("#newPwd").setAttribute('value','')
        document.querySelector("#newPwd").focus()
        return
    }
    formData.append('type','changePassword')
    formData.append('manageUsername',username)
    $.ajax(
        {
            type: 'PUT',
            url: 'http://localhost:8022/userManage',
            data: formData,
            processData: false,
            contentType: false,
            success: function (data)
            {
                if(data !== "error")
                {
                    console.log('修改成功');
                    document.getElementById('password:'+username).innerHTML = data
                    $("#editPassword").modal('hide')
                    document.querySelector("#newPwd").setAttribute('value','')
                    window.alert('修改成功')
                }
                else
                {
                    console.log('修改失败')
                    $("#editPassword").modal('hide')
                }
            },
            error: function (errorMsg)
            {
                console.log('连接错误')
                console.log(errorMsg)
            }
        }
    )
}
var state;
function onDeleteClick()
{
    state = 'delete'
    username = this.getAttribute("id").split(':').at(1)
    document.querySelector('#confirm-title').innerHTML = '删除用户'
    document.querySelector('#confirm-msg').innerHTML = '<p>确定要删除用户吗？</p>' + '<p>请注意，此操作不可逆</p>'
    $('#confirm-modal').modal('show')
}

function onResetClick()
{
    state = 'reset'
    username = this.getAttribute("id").split(':').at(1)
    document.querySelector('#confirm-title').innerHTML = '重置密码'
    document.querySelector('#confirm-msg').innerHTML = '<p>确定要重置用户吗？</p>' + '<p>重置密码为用户名+4位随机数</p>'
    $('#confirm-modal').modal('show')
}

$('#confirm-btn').bind('click',onConfirmClick)

function onConfirmClick()
{
    let formdata = new FormData();
    formdata.append('manageUsername',username)
    if(state === 'delete')
    {
        formdata.append('type','deleteUser')
        $.ajax(
        {
            type: 'PUT',
            url: 'http://localhost:8022/userManage',
            data: formdata,
            processData: false,
            contentType: false,
            success: function (data)
            {
                if(data === 'success')
                {
                    window.location='http://localhost:8022/userManage'
                }
                else
                {
                    window.showAlert('删除失败')
                }
            },
            error: function (errorMag)
            {
                window.alert('删除失败')
                console.log(errorMag)
            }
        })

    }
    else if(state === 'reset')
    {
        formdata.append('type','resetPassword')
        $.ajax(
        {
            type: 'PUT',
            url: 'http://localhost:8022/userManage',
            data: formdata,
            processData: false,
            contentType: false,
            success: function (data)
            {
                if(data !== 'error')
                {
                    window.alert(username+'的新密码为:'+data)
                    window.location='http://localhost:8022/userManage'
                }
                else
                {
                    window.alert('删除失败')
                }
            },
            error: function (errorMag)
            {
                window.alert('删除失败')
                console.log(errorMag)
            }
        })
    }
    else
    {
        console.log('错误')
    }
    state = undefined;
}

var logout = document.getElementById("btn-logout");

logout.addEventListener('click',onLogoutClick)

function onLogoutClick()
{
    console.log("logout")
    $.ajax(
        {
            type: 'GET',
            url: 'http://localhost:8022/logout',
            success: function (data)
            {
                console.log(data)
                if(data === 'success')
                {
                    window.location='http://localhost:8022/signIn'
                }
                else
                {
                    window.showAlert('注销失败')
                }
            },
            error: function (errorMag)
            {
                window.alert('注销失败')
                console.log(errorMag)
            }
        })
}
